#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
"""
@author: caijj
@version: V1.0
@file: identification_verification_code.py
@time: 2022/03/01
"""
import urllib.request
import urllib.parse
import json
import time
import base64
import requests
from jsonpath import jsonpath

from config.conf import cm

img_path = cm.tmp_file_path('nb.png')
with open(img_path, 'rb') as f:  # 以二进制读取本地图片
    data = f.read()
    encodestr = str(base64.b64encode(data), 'utf-8')
# 请求头
headers = {
    'Authorization': 'APPCODE bab7dfed269f4d7ab6b2bde8f1b9dc40',
    'Content-Type': 'application/json; charset=UTF-8'
}


def posturl(url, data={}):
    try:
        proxies = {
            "http": "http://127.0.0.1:8888",
            "https": "http://127.0.0.1:8888"
        }
        params = json.dumps(dict).encode(encoding='UTF8')
        res = requests.post(url=url, data=params, headers=headers, proxies=proxies, verify=False)
        res_json = res.json()
        word_list = jsonpath(res.json(), '$.prism_wordsInfo.word')[0]
        # req = urllib.request.Request(url, params, headers)
        # r = urllib.request.urlopen(req)

        # r.close();
        # return html.decode("utf8")
        print(res.json())
    except urllib.error.HTTPError as e:
        print(e.code)
        print(e.read().decode("utf8"))
    time.sleep(1)


if __name__ == "__main__":
    url_request = "https://ocrapi-advanced.taobao.com/ocrservice/advanced"
    dict = {'img': encodestr}

    html = posturl(url_request, data=dict)
    print(html)

